package com.fuze.energy.service.impl;

import com.fuze.energy.common.Advice;
import com.fuze.energy.common.CustomException;
import com.fuze.energy.domain.entity.MonitoringData;
import com.fuze.energy.mapper.MonitoringDataMapper;
import com.fuze.energy.mapper.WarningMapper;
import com.fuze.energy.service.TemperatureService;
import com.fuze.energy.utils.TimeUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author sepc_
 * @date 8/5/2021 13:46
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TemperatureServiceImpl implements TemperatureService {

    final MonitoringDataMapper monitoringDataMapper;

    @Autowired
    WarningMapper warningMapper;

    @Override
    public Double currentTem(String mainBoardId) {
        MonitoringData monitoringData = monitoringDataMapper.selectCurrent(mainBoardId);
        if (monitoringData == null) {
            throw new CustomException(Advice.FAILED_QUERY);
        }
        /**
         * 温度大于等于50则发出告警，记录告警信息
         * 采集器类型（温度：1；湿度：2；臭氧：3）
         */
        Double temperature = monitoringData.getTemperature();
        if (temperature.compareTo((double) 50) == 0 || temperature.compareTo((double) 50) > 0) {
            boolean HumWarningType=warningMapper.recordWarning(monitoringData.getRmuName(), temperature, 1,new Date());
            if (HumWarningType) {
                System.out.println("=======温度告警数据记录成功=======");
            } else {
                throw new CustomException(Advice.RECORD_TEM_FALL);
            }
        }
        return monitoringData.getTemperature();
    }

    @Override
    public List<List<String>> todayTem(String mainBoardId) {
        List<MonitoringData> temList = monitoringDataMapper.selectToday(TimeUtils.getBegin().toString(),
                TimeUtils.getEng().toString(),
                mainBoardId);
        ArrayList<List<String>> ints = new ArrayList<>();
        temList.forEach(data -> ints.add(Arrays.asList(TimeUtils.getTimeStr(data.getDate()), String.valueOf(data.getTemperature()))));
        return ints;
    }
}
